/**
 * 登录页脚本
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    var util = require("util");
    var md5 = require("plugins/md5");

    var loginController = {
        init: function () {
            this._updateVerifyCode();

            var username = util.getCookie("loginUserName");
            var $userName = $("#user_name");
            if (username && username != 'null' && !$userName.val()) {
                $userName.val(username);
            }

            var remember = util.getCookie("loginRemember");
            $("#remember").prop("checked", remember === "1");

            setTimeout(function () {
                $userName.focus().select();
            }, 0);
        },
        bind: function () {
            var self = this;

            var focusOrLogin = function (e) {
                if (e.keyCode == 0x0A || e.keyCode == 0x0D) {
                    if ($.trim(this.value)) {
                        $('#password').focus();
                        e.preventDefault();
                    } else {
                        self.login()
                    }
                }
            };

            var keypressLogin = function (e) {
                if (e.keyCode == 0x0A || e.keyCode == 0x0D) {
                    self.login()
                }
            };

            $('#user_name').keypress(focusOrLogin);
            $('#password').keypress(keypressLogin);
            $('#verify_code').keypress(keypressLogin);
            $("#login_btn").on("click", $.proxy(this.login, this));
            $("#verify_code_img").on("click", $.proxy(this._updateVerifyCode, this));
            $("#get_verify").on("click", $.proxy(this._updateVerifyCode, this));
        },
        /**
         * 更新验证码
         * @private
         */
        _updateVerifyCode: function () {
            $("#verify_code_img").attr('src', G_CONFIG.api_prefix + "site/verf-code&module=login&v=" + new Date().getTime());
            $('#verify_code').focus().select();
        },
        /**
         * 登录
         */
        login: function () {
            var userName = $("#user_name").val();
            var password = $("#password").val();
            var verifyCode = $("#verify_code").val();

            //记住登录账号
            if ($("#remember").prop("checked")) {
                var domain = document.domain;
                util.setCookie("loginUserName", userName, 7, '/', domain);
                util.setCookie("loginRemember", "1", 7, '/', domain);
            } else {
                util.delCookie('loginUserName');
                util.delCookie("loginRemember");
            }

            var $loginBtn = $("#login_btn");
            $loginBtn.text("登录中...").prop("disabled", true);

            var self = this;
            password = md5(password);
            util.myAjax({
                url: G_CONFIG.api_prefix + "site/login",
                type: "post",
                dataType: "json",
                data: {
                    usercode: userName,
                    password: password,
                    verfcode: verifyCode
                },
                success: function (res) {
                    if (res['retCode'] == 0) {
                        location.href = util.getUrlPara('ret_url') || '/';
                    } else {
                        self._updateVerifyCode();
                        util.alert(res["errMsg"] || res["retMsg"] || "登录失败，请检查用户名与密码");
                    }
                },
                error: function () {
                    util.alert("登录失败，请稍后再试");
                },
                complete: function () {
                    $loginBtn.text("登录").prop("disabled", false);
                }
            })
        }
    };

    loginController.init();
    loginController.bind();
});